package com.gy.api.pc.sys.authority.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.gy.api.base.*;
import com.gy.api.mobile.company.service.ICompanyService;
import com.gy.api.mobile.user.service.IUserService;
import com.gy.api.pc.sys.authority.domain.vo.*;
import com.gy.api.pc.sys.authority.service.ISysAuthorityService;
import com.gy.api.pc.sys.user.domain.vo.SysUserAddVO;
import com.gy.api.pc.user.domain.vo.CompanyUserSearchVO;
import com.gy.api.pc.user.domain.vo.CompanyUserVO;
import com.gy.api.pc.user.domain.vo.MobileUserSearchVO;
import com.gy.api.pc.user.domain.vo.MobileUserVO;
import com.gy.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.AuthorityController)
@RequestMapping(value = "/menu")
@RestController
public class AuthorityController {

    @Autowired
    private ISysAuthorityService sysAuthorityService;

    @ApiOperation(value = "查询所有权限列表(树形)")
    @GetMapping(value = "/tree")
    @SaCheckLogin
    public R<List<SysAuthorityTreeVO>> authorityTree(@ApiParam(value = "查询参数") @ModelAttribute SysAuthoritySearchVO searchVO) {
        List<SysAuthorityTreeVO> list = sysAuthorityService.getTreeList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "查询所有权限列表")
    @GetMapping(value = "/list")
    @SaCheckLogin
    public R<List<SysAuthorityVO>> getRoleAuthorityList(@ApiParam(value = "查询参数") @ModelAttribute SysAuthoritySearchVO searchVO) {
        List<SysAuthorityVO> list = sysAuthorityService.getList(searchVO);
        return new R(list);
    }



    @ApiOperation(value = "创建菜单权限")
    @PostMapping(value = "/add")
    @SaCheckLogin
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysAuthorityAddVO vo, BindingResult bindingResult) throws Exception {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            sysAuthorityService.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "修改菜单权限")
    @PostMapping(value = "/update")
    @SaCheckLogin
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysAuthorityUpdateVO vo, BindingResult bindingResult) throws Exception {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            sysAuthorityService.update(vo);
        }
        return result;
    }


    @ApiOperation(value = "删除菜单权限")
    @PostMapping(value = "/delete")
    @SaCheckLogin
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        sysAuthorityService.delete(delete.getId());
        return new R();
    }

}
